package com.lab.app.customui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.cuitrip.app.MainApplication;
import com.cuitrip.app.util.FontManager;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.UIUtil;
import com.lab.app.BaseFragment;
import com.lab.app.workflow.IUIWorkFlow;
import com.lab.rtenvironment.AppCoreRuntime;

import java.util.List;

/**
 * Created by ak
 */
public abstract class CustomUiFragment extends BaseFragment implements ICustomUiController, IUIWorkFlow {

    protected View mBaseView;

    protected View mTopbar;
    protected ViewGroup mLayoutTopbarLeftArea;
    protected ViewGroup mLayoutTopbarMiddleArea;
    protected ViewGroup mLayoutTopbarRightArea;
    protected TextView mTopbarLeftArea;
    protected TextView mTopbarMiddleArea;
    protected TextView mTopbarRightArea;
    protected View mTopbarBottomLine;

    protected FrameLayout mLayoutContent;

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == mTopbarLeftArea.getId()) {
                onTopbarLeftPress();
            } else if (id == mTopbarRightArea.getId()) {
                onTopbarRightPress();
            } else if (id == mTopbarMiddleArea.getId()) {
                onTopbarMiddlePress();
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        internalInit();
        return FontManager.changeRootViewFonts(super.onCreateView(inflater, container, savedInstanceState, mBaseView));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState, int layoutResId) {
        internalInit();
        mLayoutContent.addView(LayoutInflater.from(getActivity()).inflate(layoutResId, null));
        return FontManager.changeRootViewFonts(super.onCreateView(inflater, container, savedInstanceState, mBaseView));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState, View rootView) {
        internalInit();
        mLayoutContent.addView(rootView);
        return FontManager.changeRootViewFonts(super.onCreateView(inflater, container, savedInstanceState, mBaseView));
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initListener();
        initData();
    }

    @Override
    public void initView() {
    }

    @Override
    public void initListener() {
    }

    @Override
    public void initData() {
    }

    private void internalInit() {
        // 获取根布局
        CustomUiConfig customUiConfig = initCustomUiConfig();
        boolean isTopbarCoverStyle = (null != customUiConfig && customUiConfig.mTopbarCoverStyle);
        if (isTopbarCoverStyle) {
            mBaseView = LayoutInflater.from(AppCoreRuntime.context).inflate(R.layout.ct_custom_cover_layout, null);
        } else {
            mBaseView = LayoutInflater.from(AppCoreRuntime.context).inflate(R.layout.ct_custom_layout, null);
        }
        // 初始化Topbar
        mTopbar = UIUtil.findViewById(mBaseView, R.id.top_bar, View.class);
        mLayoutTopbarLeftArea = UIUtil.findViewById(mTopbar, R.id.layout_left_area, ViewGroup.class);
        mLayoutTopbarMiddleArea = UIUtil.findViewById(mTopbar, R.id.layout_middle_area, ViewGroup.class);
        mLayoutTopbarRightArea = UIUtil.findViewById(mTopbar, R.id.layout_right_area, ViewGroup.class);
        mTopbarLeftArea = UIUtil.findViewById(mTopbar, R.id.bar_left_item, TextView.class);
        mTopbarMiddleArea = UIUtil.findViewById(mTopbar, R.id.bar_middle_item, TextView.class);
        mTopbarRightArea = UIUtil.findViewById(mTopbar, R.id.bar_right_item, TextView.class);
        mTopbarBottomLine = UIUtil.findViewById(mTopbar, R.id.bar_bottom_line, View.class);
        // 初始化内容布局
        mLayoutContent = UIUtil.findViewById(mBaseView, R.id.layout_custom_content, FrameLayout.class);
        // 判断是否CoverStyle
        /*if (isTopbarCoverStyle) {
            mTopbar.setBackgroundResource(R.drawable.transparent);
            mTopbarBottomLine.setVisibility(View.INVISIBLE);
            mTopbarLeftArea.setText(R.string.back);
            mTopbarLeftArea.setTextColor(getResources().getColor(R.color.color_white));
            mTopbarRightArea.setTextColor(getResources().getColor(R.color.color_white));
            Drawable drawable = getResources().getDrawable(R.drawable.global_icon_back_white_selector);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            mTopbarLeftArea.setCompoundDrawables(drawable, null, null, null);
        }*/
        if (customUiConfig != null && customUiConfig.mDisMissTopbarBottomLine) {
            mTopbarBottomLine.setVisibility(View.INVISIBLE);
        }
        // 事件绑定
        UIUtil.bindClickListener(mOnClickListener, mTopbarLeftArea, mTopbarRightArea, mTopbarMiddleArea);
        // 初始化自定义控件配置样式
        performCustomUiConfig();
        if (initCloseTopBar()) {
            hideTopBar();
        }
    }

    private void performCustomUiConfig() {
        CustomUiConfig config = initCustomUiConfig();
        if (null != config) {
            // 处理Topbar配置
            // 处理AreaStyle
            CustomUiConfig.BarStyle topBarStyle = config.mTopbarStyle;
            if (null != topBarStyle) {
                if (topBarStyle == CustomUiConfig.BarStyle.ORANGE_STYLE) {
                    mTopbar.setBackgroundColor(ResourceUtil.getThemeColor());
                    mTopbarLeftArea.setTextColor(getResources().getColor(R.color.ct_white));
                    mTopbarMiddleArea.setTextColor(getResources().getColor(R.color.ct_white));
                    mTopbarRightArea.setTextColor(getResources().getColor(R.color.ct_white));
                } else if (topBarStyle == CustomUiConfig.BarStyle.WHITE_STYLE) {
                    mTopbar.setBackgroundColor(getResources().getColor(R.color.ct_white));
                    mTopbarLeftArea.setTextColor(ResourceUtil.getThemeColor());
                    mTopbarMiddleArea.setTextColor(getResources().getColor(R.color.ct_black));
                    mTopbarRightArea.setTextColor(ResourceUtil.getThemeColor());
                }
            }

            CustomUiConfig.AreaStyle leftAreaStyle = config.mLeftStyle;
            if (leftAreaStyle == CustomUiConfig.AreaStyle.ICON_STYLE) {
                mTopbarLeftArea.setTextSize(21.f);
                mTopbarLeftArea.setTypeface(MainApplication.getIconFont());
                FontManager.setViewFont(mTopbarRightArea, MainApplication.getIconFont());
            } else if (leftAreaStyle == CustomUiConfig.AreaStyle.TEXT_STYLE) {
                mTopbarLeftArea.setTextSize(17.f);
                FontManager.setViewFont(mTopbarRightArea, FontManager.getInstance().getDefaultType());
            }

            CustomUiConfig.AreaStyle rightAreaStyle = config.mRightStyle;
            if (rightAreaStyle == CustomUiConfig.AreaStyle.ICON_STYLE) {
                mTopbarRightArea.setTextSize(21.f);
                FontManager.setViewFont(mTopbarRightArea, MainApplication.getIconFont());
                mTopbarRightArea.setTypeface(MainApplication.getIconFont());
            } else if (rightAreaStyle == CustomUiConfig.AreaStyle.TEXT_STYLE) {
                mTopbarRightArea.setTextSize(17.f);
                FontManager.setViewFont(mTopbarRightArea, FontManager.getInstance().getDefaultType());
            }


            // 处理中区域
            if (null != config.mTopbarMiddleAreaName) {
                mTopbarMiddleArea.setText(config.mTopbarMiddleAreaName);
                //bug patch : FontManager 无法起效
                FontManager.setViewFont(mTopbarMiddleArea, FontManager.getInstance().getDefaultType());
            }
            if (null != config.mTopbarLeftAreaName) {
                if (config.mTopbarLeftAreaName.length() > 8){
                    mTopbarLeftArea.setTextSize(13.f);
                }
                mTopbarLeftArea.setText(config.mTopbarLeftAreaName);
            }
            // 处理右区域
            if (null != config.mTopbarRightAreaName) {
                if (config.mTopbarRightAreaName.length() > 8){
                    mTopbarRightArea.setTextSize(13.f);
                }
                mTopbarRightArea.setText(config.mTopbarRightAreaName);
            }
            // 处理文本超出样式
            if (config.mTopbarMidderAreaEllipsizeEnd) {
                mTopbarMiddleArea.setEllipsize(TextUtils.TruncateAt.END);
            }
        }
        // 更新Topbar区域尺寸,以适应内容
        updateTopbarAreaSize();
    }

    private void updateTopbarAreaSize() {
        mTopbar.post(new Runnable() {
            @Override
            public void run() {
                int layoutTopbarLeftAreaRealWidth = mLayoutTopbarLeftArea.getMeasuredWidth();
                int layoutTopbarRightAreaRealWidth = mLayoutTopbarRightArea.getMeasuredWidth();
                int maxWidth = Math.max(layoutTopbarLeftAreaRealWidth, layoutTopbarRightAreaRealWidth);
                mLayoutTopbarLeftArea.setMinimumWidth(maxWidth);
                mLayoutTopbarRightArea.setMinimumWidth(maxWidth);
            }
        });
    }

    @Override
    public void showTopBar() {
        mTopbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideTopBar() {
        mTopbar.setVisibility(View.GONE);
    }


    @Override
    public void onTopbarLeftPress() {
    }

    @Override
    public void onTopbarRightPress() {
    }

    @Override
    public void onTopbarMiddlePress() {
    }

    @Override
    public void update(CustomUiUpdateParam param) {
        if (null != param) {
            List<CustomUiUpdateParam.IUpdateParam> updateParamList = param.getUpdateParamList();
            if (null != updateParamList) {
                for (CustomUiUpdateParam.IUpdateParam updateParam : updateParamList) {
                    // 主标题
                    if (updateParam instanceof CustomUiUpdateParam.Title) {
                        CustomUiUpdateParam.Title updateParamTitle = (CustomUiUpdateParam.Title) updateParam;
                        String name = updateParamTitle.name;
                        if (null == name) {
                            mTopbarMiddleArea.setText("");
                        } else {
                            mTopbarMiddleArea.setText(name);
                        }
                    }
                    // 左区域文本
                    else if (updateParam instanceof CustomUiUpdateParam.LeftArea) {
                        CustomUiUpdateParam.LeftArea updateParamLeftArea = (CustomUiUpdateParam.LeftArea) updateParam;
                        String name = updateParamLeftArea.name;
                        if (null == name) {
                            mTopbarLeftArea.setText("");
                        } else {
                            mTopbarLeftArea.setText(name);
                        }
                    }
                    // 右区域文本
                    else if (updateParam instanceof CustomUiUpdateParam.RightArea) {
                        CustomUiUpdateParam.RightArea updateParamRightArea = (CustomUiUpdateParam.RightArea) updateParam;
                        String name = updateParamRightArea.name;
                        if (null == name) {
                            mTopbarRightArea.setText("");
                        } else {
                            mTopbarRightArea.setText(name);
                        }
                    }
                }
                // 更新Topbar区域尺寸,以适应内容
                updateTopbarAreaSize();
            }
        }
    }

    @Override
    public void update(CustomUiConfig param) {
        if (param != null && param.mTopbarBottomLineChangedHeightPx > 0 && !param.mDisMissTopbarBottomLine) {
            mTopbarBottomLine.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams layoutParams = mTopbarBottomLine.getLayoutParams();
            layoutParams.height = param.mTopbarBottomLineChangedHeightPx;
            mTopbarBottomLine.setLayoutParams(layoutParams);
        }
    }

}
